package com.aoxin.service.impl.opm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.opm.SlfOrgMapper;
import com.aoxin.mapper.opm.SlfSchoolMapper;
import com.aoxin.model.dto.TreeDto;
import com.aoxin.model.opm.SlfOrg;
import com.aoxin.service.inter.opm.OrgService;
import com.aoxin.util.ErrorUtil;

@Service("orgService")
public class OrgServiceImpl implements OrgService {

	@Autowired
	private SlfOrgMapper slfOrgMapper;

	@Autowired
	private SlfSchoolMapper slfSchoolMapper;

	@Override
	@Transactional
	public void insertSlfOrg(SlfOrg org) {
		Assert.notNull(org, String.format(ErrorUtil.PARAMETER_NULL, "org"));
		org.setCreateDate(new Date());
		if (null == org.getSequence()) {
			org.setSequence(this.slfOrgMapper.getNextSequence(org.getPartId()).intValue());
		}
		if (this.slfOrgMapper.checkNameOrAddrCode(org.getName(), null, null) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, org.getName()));
		}
		this.slfOrgMapper.insertSelective(org);
	}

	@Override
	@Transactional
	public void deleteSlfOrg(Integer orgId) {
		Assert.notNull(orgId, String.format(ErrorUtil.PARAMETER_NULL, "orgId"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("partId", orgId);
		if (this.slfOrgMapper.getRecordCount(map) > 0L) {
			throw new IllegalArgumentException(ErrorUtil.HAS_PARENT_ORG);
		}
		map.clear();
		map.put("orgId", orgId);
		if (this.slfSchoolMapper.getRecordCount(map) > 0L) {
			throw new IllegalArgumentException(ErrorUtil.HAS_SCHOOL);
		}
		this.slfOrgMapper.deleteByPrimaryKey(orgId);
	}

	@Override
	@Transactional
	public void deleteSlfOrgByIds(List<Integer> orgIds) {
		Assert.notEmpty(orgIds, String.format(ErrorUtil.PARAMETER_NULL, "orgIds"));
		Map<String, Object> map = new HashMap<String, Object>();
		for (Integer integer : orgIds) {
			map.clear();
			map.put("partId", integer);
			if (this.slfOrgMapper.getRecordCount(map) > 0) {
				throw new IllegalArgumentException(ErrorUtil.HAS_PARENT_ORG);
			}
			map.clear();
			map.put("orgId", integer);
			if (this.slfSchoolMapper.getRecordCount(map) > 0L) {
				throw new IllegalArgumentException(ErrorUtil.HAS_SCHOOL);
			}
			
		}
		this.slfOrgMapper.deleteByPrimaryKeys(orgIds);
	}

	@Override
	@Transactional
	public void updateSlfOrg(SlfOrg org) {
		Assert.notNull(org, String.format(ErrorUtil.PARAMETER_NULL, "org"));
		if (this.slfOrgMapper.checkNameOrAddrCode(org.getName(), null, org.getOrgId()) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, org.getName()));
		}
		Integer a = slfSchoolMapper.IsExistOrgId(org.getOrgId());
		if(a > 0){
			throw new IllegalArgumentException(String.format(ErrorUtil.EXISTORGIDUPDATE));
		}
		this.slfOrgMapper.updateByPrimaryKeySelective(org);
	}

	@Override
	@Transactional
	public void updateSlfOrgStates(List<Integer> orgIds, Integer state) {
		Assert.notEmpty(orgIds, String.format(ErrorUtil.PARAMETER_NULL, "orgIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
		//判断是否存在校区(禁用的情况下)
		if(state == 0){
		for(Integer orgId : orgIds){
			Integer a = slfSchoolMapper.IsExistOrgId(orgId);
			if(a > 0){
				throw new IllegalArgumentException(String.format(ErrorUtil.EXISTORGID));
			}
		  }
		}
		this.slfOrgMapper.updateStates(orgIds, state);
	}

	@Override
	public SlfOrg getSlfOrg(Integer orgId) {
		Assert.notNull(orgId, String.format(ErrorUtil.PARAMETER_NULL, "orgId"));
		SlfOrg slfOrg = this.slfOrgMapper.selectByPrimaryKey(orgId);
		return slfOrg;
	}
	
	@Override
	public List<SlfOrg> getSlfOrgList(Integer orgId,Integer state){
		Map<String,Object> params = new HashMap<String,Object>();
		if(state == null || "".equals(state)){
		params.put("state",1);
		state = 1;
	    }else{
	    params.put("state",state);	
	    }
		if(state == 1){
		if(orgId == null){
			params.put("partId", "0");
			List<SlfOrg> slfOrgs = this.slfOrgMapper.getList(params);
			packagingOrg(slfOrgs, params);
			return slfOrgs;
		}else{
			params.put("orgId", orgId);
			List<SlfOrg> slfOrgs = this.slfOrgMapper.getList(params);
			packagingOrg(slfOrgs, params);
			return slfOrgs;
		}
		}else{
			List<SlfOrg> slfOrgs = this.slfOrgMapper.getListWx(params);
			return slfOrgs;
		}
	}
	/**
	 * 递归查询树
	 * 
	 * @param SlfOrgs
	 *            机构集合
	 * @param params
	 *            条件集合
	 */
	private void packagingOrg(List<SlfOrg> SlfOrgs, Map<String, Object> params) {
		if (SlfOrgs.size() > 0) {
			for (SlfOrg SlfOrg : SlfOrgs) {
				params.put("orgId",null);
				params.put("partId", SlfOrg.getOrgId());
				List<SlfOrg> trees = this.slfOrgMapper.getList(params);
				if(trees.size() > 0){
					SlfOrg.setChildren(trees);
				}else{
					SlfOrg.setChildren(null);
				}
				packagingOrg(trees, params);
			}
		}
	}
	
	private void packagingOrgOther(List<SlfOrg> SlfOrgs, Map<String, Object> params) {
		if (SlfOrgs.size() > 0) {
			for (SlfOrg SlfOrg : SlfOrgs) {
				params.put("partId", SlfOrg.getValue());
				List<SlfOrg> trees = this.slfOrgMapper.getListOther(params);
				if(trees.size() > 0){
					SlfOrg.setChildren(trees);
				}else{
					SlfOrg.setChildren(null);
				}
				packagingOrgOther(trees, params);
			}
		}
	}

	@Override
	public Long getNextSequence(Integer partId) {
		return this.slfOrgMapper.getNextSequence(partId);
	}

	@Override
	public List<SlfOrg> getOrgTreeList(Integer partId) {
		/*partId = 75;
		List<TreeDto> treeDtos = new ArrayList<TreeDto>();
		SlfOrg slfOrg = this.slfOrgMapper.selectByPrimaryKey(partId);
		treeDtos = this.slfOrgMapper.getOrgTreeList(slfOrg.getPartId());
		packagingOrgTree(treeDtos);
		return treeDtos;*/
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("partId", "0");
		List<SlfOrg> slfOrgs = this.slfOrgMapper.getListOther(params);
		packagingOrgOther(slfOrgs, params);
		return slfOrgs;
	}

	/**
	 * 递归查询机构树
	 * 
	 * @param treeDtos
	 */
	private void packagingOrgTree(List<TreeDto> treeDtos) {
		if (treeDtos.size() > 0) {
			for (TreeDto treeDto : treeDtos) {
				List<TreeDto> trees = this.slfOrgMapper.getOrgTreeList(Integer.parseInt(treeDto.getValue()));
				treeDto.setChildren(trees);
				packagingOrgTree(trees);
			}
		}
	}
}
